//@author wtfumlomglol
package amm.Graphic;

import java.io.*;
import java.util.*;

//lampa iranyito rendszert leiro osztaly
public class LightController {

    private TrafficLight[] lights = new TrafficLight[4];
    //annak a lampanak a sorszama, ami eppen zold
    private int lightActive = 0;
    private XRoads xr;
    private Logger logger;

    //konstruktor határozza meg, hogy melyik lámpra induljon zöldként
    public LightController(Compass c, XRoads xroads) {
        xr = xroads;
        
        for (int i = 0; i < 4; i++) {
            lights[i] = new TrafficLight(this);
        }
        switch (c) {
            case EAST:
                lights[0].setColour(true);
                lightActive=0;
                break;
            case NORTH:
                lights[1].setColour(true);
                lightActive=1;
                break;
            case WEST:
                lights[2].setColour(true);
                lightActive=2;
                break;
            case SOUTH:
                lights[3].setColour(true);
                lightActive=3;
                break;
        }
    }

    //lampak hivjak meg, ha letelt a zold
    //allapotukra kiszabott idejuk
    public void notifyLC() {
        //atvaltja a hivo lampat pirosra
        logger=xr.getLogger();
        lights[lightActive].swapColour();
        switch (lightActive) {
            case 0:
                logger.addMessage("TrafficLight[0] on " + xr.getName() + " turned red facing [EAST]");
                break;
            case 1:
                logger.addMessage("TrafficLight[1] on " + xr.getName() + " turned red facing [NORTH]");
                break;
            case 2:
                logger.addMessage("TrafficLight[2] on " + xr.getName() + " turned red facing [WEST]");
                break;
            case 3:
                logger.addMessage("TrafficLight[3] on " + xr.getName() + " turned red facing [SOUTH]");
                break;
        }
        //csereli a zold lampa sorszamat
        lightActive--;
        lightActive = (lightActive + 4) % 4;
        //beallitja a kovetkezo lampa szinet zoldre
        lights[lightActive].swapColour();
        switch (lightActive) {
            case 0:
                logger.addMessage("TrafficLight[0] on " + xr.getName() + " turned green facing [EAST]");
                break;
            case 1:
                logger.addMessage("TrafficLight[1] on " + xr.getName() + " turned green facing [NORTH]");
                break;
            case 2:
                logger.addMessage("TrafficLight[2] on " + xr.getName() + " turned green facing [WEST]");
                break;
            case 3:
                logger.addMessage("TrafficLight[3] on " + xr.getName() + " turned green facing [SOUTH]");
                break;
        }
    }

    //a zöld lámpa helyét adja vissza
    public int getActive()
    {
        return lightActive;
    }

    public TrafficLight getLight(int i) {
        return lights[i];
    }
}
